package com.rasenhund.android.grecorder.activity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.rasenhund.android.grecorder.R;
import com.rasenhund.android.grecorder.common.Const;
import com.rasenhund.android.grecorder.dao.RecordDao;
import com.rasenhund.android.grecorder.helper.DbHelper;

/**
 * 勝敗選択画面
 */
public class SelectResultActivity extends ConfirmBaseActivity {
    @SuppressWarnings("unused")
    private static final String TAG = SelectResultActivity.class.getSimpleName();
    private final SelectResultActivity self = this;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_result);
        
        // 圧勝ボタン
        Button btnPerfectWin = (Button) findViewById(R.id.btnPerfectWin);
        btnPerfectWin.setOnClickListener(btnPerfectWin_OnClickListener);

        // 勝利ボタン
        Button btnWin = (Button) findViewById(R.id.btnWin);
        btnWin.setOnClickListener(btnWin_OnClickListener);

        // 引き分けボタン
        Button btnDraw = (Button) findViewById(R.id.btnDraw);
        btnDraw.setOnClickListener(btnDraw_OnClickListener);

        // 敗北ボタン
        Button btnLose = (Button) findViewById(R.id.btnLose);
        btnLose.setOnClickListener(btnLose_OnClickListener);
        
        // 完敗ボタン
        Button btnPerfectLose = (Button) findViewById(R.id.btnPerfectLose);
        btnPerfectLose.setOnClickListener(btnPerfectLose_OnClickListener);

    }

    /********************
     * EventListener
     ********************/

    /**
     * 圧勝ボタン押下時のEventListener
     */
    final OnClickListener btnPerfectWin_OnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {

            setResultCd(Const.RESULT.PERFECT_WIN);
            jumpMenu();

        }
    };

    /**
     * 勝利ボタン押下時のEventListener
     */
    final OnClickListener btnWin_OnClickListener = new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            
            setResultCd(Const.RESULT.WIN);
            jumpMenu();
            
        }
    };

    /**
     * 引き分けボタン押下時のEventListener
     */
    final OnClickListener btnDraw_OnClickListener = new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            
            setResultCd(Const.RESULT.DRAW);
            jumpMenu();
            
        }

    };
    
    /**
     * 敗北ボタン押下時のEventListener
     */
    final OnClickListener btnLose_OnClickListener = new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            
            setResultCd(Const.RESULT.LOSE);
            jumpMenu();
            
        }
    };

    /**
     * 完敗ボタン押下時のEventListener
     */
    final OnClickListener btnPerfectLose_OnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {

            setResultCd(Const.RESULT.PERFECT_LOSE);
            jumpMenu();

        }
    };

    /********************
     * Method
     ********************/
    /**
     * 結果コードを登録
     * 
     * @param resultCd
     */
    private void setResultCd(int resultCd){
        
        // パラメータ用変数に結果コードをセット
        mRecord.setResultCd(resultCd);
        
        // DBへ登録
        if (registRecord()) {

            String sMsg = getResources().getString(R.string.msg_regist_complete);
            Toast.makeText(self, sMsg, Toast.LENGTH_SHORT).show();

        }
    }
    
    /**
     * 戦歴の登録処理
     */
    private boolean registRecord() {

        DbHelper dbHelper = null;
        SQLiteDatabase db = null;

        long iRet;

        try {

            dbHelper = new DbHelper(self);
            db = dbHelper.getWritableDatabase();
            RecordDao dao = new RecordDao(db);

            iRet = dao.insert(mRecord);

        } finally {
            if (db.isOpen()) {
                db.close();
            }
        }
        
        if (iRet == 0) {
            return false;
        }

        return true;
    }
    
    /**
     * メニューへ戻る
     * @param category
     */
    private void jumpMenu(){
        
        Intent intent = new Intent(self, MenuActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        
    }
    
}
